//
//  LZZanAndCollectStatusPresenter.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/12/7.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZZanAndCollectStatusPresenter.h"
#import "LZVideoListModel.h"

@implementation LZZanAndCollectStatusPresenter

+ (instancetype)shareInstance {
    static LZZanAndCollectStatusPresenter *lzZanAndCollectStatusPresenter;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        lzZanAndCollectStatusPresenter = [[LZZanAndCollectStatusPresenter alloc] init];
    });
    
    return lzZanAndCollectStatusPresenter;
}

#pragma mark - 视频的收藏状态的操作
// ************************************* 视频的收藏状态的操作 *************************************

- (NSMutableArray*)collectVideoArray {
    if (_collectVideoArray) {
        return _collectVideoArray;
    }
    _collectVideoArray = [NSMutableArray array];
    return _collectVideoArray;
}

// 收藏状态改变的动作
- (void)addCollectVideoWithId:(NSString*)videoId andCollectStatus:(BOOL)isStore{
    LZCollectStatusModel *model = nil;
    NSArray *result = [self.collectVideoArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.collectId = %@",videoId]];
    if (result.count>0) {
        model = result[0];
    }
    else {
        model = [[LZCollectStatusModel alloc] init];
        model.collectId = [videoId copy];
        [self.collectVideoArray addObject:model];
    }
    
    model.isStore = isStore;
}

#pragma mark - 视频的👍的状态的操作
// ************************************* 视频的👍的状态的操作 *************************************

- (NSMutableArray*)zanArray {
    if (_zanArray) {
        return _zanArray;
    }
    
    _zanArray = [NSMutableArray array];
    return _zanArray;
}

- (void)addZanVideoWithId:(NSString*)videoId {
    if (self.zanArray.count==0) {
        [self.zanArray addObject:videoId];
    }
    else {
        NSArray *result = [self.zanArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self = %@",videoId]];
        if (result.count==0) {
            [self.zanArray addObject:videoId];
        }
    }
}

- (void)resetArray {
    [self.zanArray removeAllObjects];
    [self.collectVideoArray removeAllObjects];
}



// 更新数据源
- (BOOL)updateTheModelZanStateWithModelArr:(NSArray *)modelArr
{
    BOOL isChanged = NO;
    for (NSString *idStr in self.zanArray) {
        NSArray *result = [modelArr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.videoId = %@",idStr]];
        if (result.count>0) {
            isChanged = YES;
            LZVideoListModel *model = result[0];
            if (!model.isLiked) {
                model.isLiked = YES;
                model.likesCount += 1;
            }
        }
    }
    return isChanged;
}

- (BOOL)updateTheModelCollectStateWithModleArr:(NSArray *)modelArr
{
    BOOL isChanged = NO;

    for (LZCollectStatusModel *statusModel in self.collectVideoArray) {
        
        NSArray *result = [modelArr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.videoId = %@",statusModel.collectId]];
        if (result.count>0) {
            LZVideoListModel *model = result[0];
            if (model.isStore != statusModel.isStore) {
                isChanged = YES;
                model.isStore = statusModel.isStore;
                model.storeCount += statusModel.isStore?1:(-1);
            }
        }
    }
    return isChanged;
}

#pragma mark - 餐厅的收藏状态的操作
// ************************************* 餐厅的收藏状态的操作 *************************************
- (NSMutableArray*)collectRestaurantArray {
    if (_collectRestaurantArray) {
        return _collectRestaurantArray;
    }
    
    _collectRestaurantArray = [NSMutableArray array];
    return _collectRestaurantArray;
}

- (void)addCollectRsetaurantWithId:(NSString *)restaurantId andCollectStatus:(BOOL)isStore {
    if (!self.isStartRecordRestaurantCollectStatus) {
        return;
    }
    LZCollectStatusModel *model = nil;
    NSArray *result = [self.collectRestaurantArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.collectId = %@",restaurantId]];
    if (result.count>0) {
        model = result[0];
    }
    else {
        model = [[LZCollectStatusModel alloc] init];
        model.collectId = [restaurantId copy];
        [self.collectRestaurantArray addObject:model];
    }
    
    model.isStore = isStore;
}

- (void)setIsStartRecordRestaurantCollectStatus:(BOOL)isStartRecordRestaurantCollectStatus {
    _isStartRecordRestaurantCollectStatus = isStartRecordRestaurantCollectStatus;
    if (!isStartRecordRestaurantCollectStatus) {
        [self.collectRestaurantArray removeAllObjects];
    }
}

@end

@implementation LZCollectStatusModel
@end

